<?php
/**
 * ThemeController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** action cacher */
require_once 'Madoqua/Cache/Action.php';

/**
 * BlogAdmin_ThemeController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_ThemeController extends Madoqua_Controller_Action_Abstract
{
    /**
     * initialization
     */
    public function init() {
        parent::init();
        $this->_modelLoader->load('Theme_Theme');
        //load the model
    }

    /**
     * overview of theme related nonsense
     */
    public function indexAction()
    {
        $theme = Theme_Theme::get();
        //get current theme

        $this->view->theme = $theme;

        $form = $this->_formLoader->get('BlogAdmin_Theme_Settings', array('theme' => $theme));
        $this->view->form = $form;
        //load form and assign it to view
        
        if ($form->isPosted() && $form->isValid($this->getRequest()->getPost())) {
            //valid form post
            
            Theme_Theme::saveUserConfig($theme->key, $form->getValues());
            //save the user config
            
            $this->getHelper('redirector')->gotoRoute(
                array(
                    'controller' => 'theme',
                    'action' => 'index'
                ),
                'blog_admin'                
            );
            //redirect back
        }
    }

    /**
     * select a new theme
     */
    public function selectAction()
    {
        if ($this->getRequest()->isPost()) {
            //we have a post! \ó/

            $key = $this->getRequest()->getPost('key');
            if (empty($key)) {
                throw new Exception('"key" not set in post request.');
            }
            //check whether key is set

            $theme = Theme_Theme::get($key);
            //fetch the theme

            if (!$theme) {
                throw new Exception('theme: ' . $key . ' is not valid');
            }
            //check whether it is valid

            Madoqua_Cache_Action::get()->clean();
            
            $this->_config->theme->active = $key;
            $this->_modelLoader->load(array('BlogAdmin_Config'));
            BlogAdmin_Config::saveApplicationConfig();
            //update the config
            
            $this->getHelper('redirector')->gotoRouteAndExit(array(), 'blog_admin');
            //redirect back :)
        }

        $this->view->themes = Theme_Theme::getList();
        $this->view->current = $this->_config->theme->active;
        //list of themes and the current theme
    }

    /**
     * modify a theme's settings
     */
    public function modifyAction()
    {

    }
}